#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pr;
typedef vector<long long> vll;
#define vi vector<int>
#define vvi vector<vi>
#define ff first
#define ss second
#define vvl vector<vll>
#define pb push_back
#define ho(a) a.begin(), a.end()
#define trace(arr) \
for (int i = 0; i < arr.size(); i++) \
{ \
cout << arr[i] << ' '; \
} \
cout << endl;
#define countsetbit(n) __builtin_popcountll(n)
ll aj = LLONG_MAX;
ll mod = 1e9 + 7;
ll mod2 = 998244353;
bool cmp(pr a, pr b)
{
return a.second < b.second;
}
ll binpow3(ll a, ll b)
{
ll res = 1;
a = a % mod;
while (b > 0)
{
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b = b >> 1;
}
return res;
}
string numtostring(ll n)
{
return bitset<31>(n).to_string();
}
unsigned long stringtonum(string s)
{
return bitset<31>(s).to_ulong();
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b)
{
return (a.second < b.second);
}
int poww(int a, int b)
{
int ans = 1;
while (b)
{
if (b & 1)
{
ans = (ans * 1LL * a) % mod;
}
a = (a * 1LL * a) % mod;
b >>= 1;
}
return ans;
}
int mull(ll a, ll b)
{
int ans = 0;
while (b)
{
if (b & 1)
{
ans = (ans + a) % mod;
}
a = (a + a) % mod;
b >>= 1;
}
return ans;
}
bool BipartiteDfs(ll node, vvl &adj, vll &vis)
{
for (auto it : adj[node])
{
if (vis[it] == -1)
{
vis[it] = 1 - vis[node];
if (!BipartiteDfs(it, adj, vis))
{
return false;
}
}
else if (vis[node] == vis[it])
{
return false;
}
}
return true;
}
bool is_prime(int n)
{
if (n == 2)
{
return true;
}
for (int i = 2; i <= sqrt(n); i++)
{
if (n % i == 0)
{
return false;
}
}
return true;
}
void printPrimeFactors(int n, vll &fac)
{
while (n % 2 == 0)
{
fac.pb(2);
n = n / 2;
}
for (int i = 3; i <= sqrt(n); i = i + 2)
{
while (n % i == 0)
{
fac.pb(i);
n = n / i;
}
}
if (n > 2)
fac.pb(n);
}
bool cmp2(pr &p1, pr &p2)
{
if (p1.first == p2.first)
return p1.second < p2.second;
return p1.first > p2.first;
}
long long nearestPowerOf2(long long N)
{
long long a = log2(N);
if (pow(2, a) == N)
return N;
return pow(2, a + 1);
}
void solve()
{
ll n;
cin >> n;
map<ll, ll> mp;
// vll v(n);
ll mini = LLONG_MAX;
// int id = 0;
// vector<vector<ll>> v(n);
vll ans;
for (int i = 0; i < n; i++)
{
int n1;
cin >> n1;
vll v1(n1);
int id = 0;
for (int j = 0; j < n1; j++)
{
cin >> v1[j];
// v[i].pb(v1[j]);
if (mini > v1[j])
{
mini = v1[j];
// id = i;
}
}
ll smallest = LLONG_MAX;
ll secondM = LLONG_MAX;
for (int j = 0; j < n1; j++)
{
if (v1[j] < smallest)
{
smallest = v1[j];
id = j;
}
}
for (int j = 0; j < n1; j++)
{
if (v1[j] < secondM && v1[j] >= smallest && id != j)
{
secondM = v1[j];
}
}
// if (secondM == LLONG_MAX)
// {
// ans.pb(smallest);
// }
ans.pb(secondM);
}
sort(ho(ans));
reverse(ho(ans));
ll sum = 0;
// trace(ans);
for (int i = 0; i < n - 1; i++)
{
sum += ans[i];
}
sum += mini;
cout << sum << "\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
1356. Sort Integers by The Number of 1 Bits | 922. Sort Array By Parity II |
344. Reverse String | 1047. Remove All Adjacent Duplicates In String |
977. Squares of a Sorted Array | 852. Peak Index in a Mountain Array |
461. Hamming Distance | 1748. Sum of Unique Elements |
897. Increasing Order Search Tree | 905. Sort Array By Parity |
1351. Count Negative Numbers in a Sorted Matrix | 617. Merge Two Binary Trees |
1450. Number of Students Doing Homework at a Given Time | 700. Search in a Binary Search Tree |
590. N-ary Tree Postorder Traversal | 589. N-ary Tree Preorder Traversal |
1299. Replace Elements with Greatest Element on Right Side | 1768. Merge Strings Alternately |
561. Array Partition I | 1374. Generate a String With Characters That Have Odd Counts |
1822. Sign of the Product of an Array | 1464. Maximum Product of Two Elements in an Array |
1323. Maximum 69 Number | 832. Flipping an Image |
1295. Find Numbers with Even Number of Digits | 1704. Determine if String Halves Are Alike |
1732. Find the Highest Altitude | 709. To Lower Case |
1688. Count of Matches in Tournament | 1684. Count the Number of Consistent Strings |